package com.softlook.mt.server.db.dao;

import java.util.List;
import java.util.LinkedList;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.StringBuffer;
import jp.crossfire.framework.database.ICondition;
import java.sql.Connection;
import jp.crossfire.framework.database.SQLExecutor;
import com.softlook.mt.server.db.internal.ClientManagerGetListner;
import jp.crossfire.framework.database.ConditionContainer;
import jp.crossfire.framework.database.Values;
import jp.crossfire.framework.database.Condition;
import com.softlook.mt.server.db.internal.ClientManagerInternal;

public class ClientManager extends ClientManagerInternal {
	private List client = new LinkedList();
	public List getClient() {
		return this.client;
	}
	public void setClient(List client) {
		this.client = client;
	}
	public String getSql(Hashtable input, Integer limit, Integer offset) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("SELECT ");
		buf.append("*");
		
		buf.append(" FROM ");
		buf.append("client");
		
		// Condition
		ICondition condition = getGetCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" WHERE " + condition.toString());
		}
		
		// GROUP BY
		
		// Having Condition
		condition = getGetHavingCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" HAVING " + condition.toString());
		}
		
		// ORDER
		
		// Limit
		if(limit != null && offset != null){
			buf.append(" LIMIT " + offset.toString() +", " + limit.toString());
		}
		
		return buf.toString();
	}
	public List get(Connection con, Hashtable input, Integer limit, Integer offset) throws jp.crossfire.framework.database.HyperDbException {
		this.reset();
		String sql = getSql(input, limit, offset);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		// Listners
		List listners = new LinkedList();
		
		ClientManagerGetListner listner0 = (new ClientManagerGetListner());
		listners.add(listner0);
		
		// Execute Query
		exec.executeSelectSQL(sql, listners);
		listner0.orMap();
		
		this.client = listner0.clientTmpCache;
		return listner0.result;
	}
	public String addSql(Hashtable input) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("INSERT INTO ");
		buf.append("client");
		
		ConditionContainer cnd0= new Values("VALUES");
		ConditionContainer cnd1 = new ConditionContainer(",");
		ConditionContainer cnd2 = new ConditionContainer(",");
		cnd0.addChild(cnd1);
		cnd0.addChild(cnd2);
		Condition cnd3 = new Condition("name");
		cnd1.addChild(cnd3);
		Condition cnd4 = new Condition("'$name'");
		if(input.get("name") != null){
			((Condition)cnd4).getProperties().put("name", input.get("name"));
		}
		cnd2.addChild(cnd4);
		if(!cnd0.toString().equals("")){
			buf.append(" " + cnd0.toString());
		}
		
		return buf.toString();
	}
	public void add(Connection con, Hashtable input) throws jp.crossfire.framework.database.HyperDbException {
		String sql = addSql(input);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		exec.execUpdateSQL(sql);
	}
	public String setSql(Hashtable input) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("UPDATE ");
		buf.append("client");
		
		// SET
		ICondition cnd0= new ConditionContainer(",");
		Condition cnd1 = new Condition("name = '$name'");
		if(input.get("name") != null){
			((Condition)cnd1).getProperties().put("name", input.get("name"));
		}
		((ConditionContainer)cnd0).addChild(cnd1);
		if(!cnd0.toString().equals("")){
			buf.append(" SET " + cnd0.toString());
		}
		
		// Condition
		ICondition condition = setGetCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" WHERE " + condition.toString());
		}
		
		return buf.toString();
	}
	public void set(Connection con, Hashtable input) throws jp.crossfire.framework.database.HyperDbException {
		String sql = setSql(input);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		exec.execUpdateSQL(sql);
	}
	public String delSql(Hashtable input) {
		StringBuffer buf = new StringBuffer();
		
		buf.append("DELETE FROM ");
		buf.append("client");
		
		// Condition
		ICondition condition = delGetCondition(input);
		if(condition != null && !condition.toString().equals("")){
			buf.append(" WHERE " + condition.toString());
		}
		
		return buf.toString();
	}
	public void del(Connection con, Hashtable input) throws jp.crossfire.framework.database.HyperDbException {
		String sql = delSql(input);
		SQLExecutor exec = new SQLExecutor();
		exec.setCon(con);
		
		exec.execUpdateSQL(sql);
	}
	public void reset() {
			this.client.clear();
	}
}
